//
//  ST_AIMatchSuccessViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import NIMSDK
import SwiftyJSON


class ST_AIMatchSuccessViewController: UIViewController {

    var json = JSON.null
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = Color.init(hexString: "#FD7078")
        
        view.addSubview(pair_title_label)
        view.addSubview(pairContentLabel)
        
        
        
        pair_title_label.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(-100)
            make.centerX.equalToSuperview()
        }
        
        pairContentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(pair_title_label.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        
        
        
        
        view.addSubview(heart_image_view)
        heart_image_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(pairContentLabel.snp.bottom).offset(64)
        }
        
        view.addSubview(left_image_view)
        left_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(100)
            make.top.equalTo(heart_image_view).offset(-40)
            make.right.equalTo(heart_image_view.snp.left).offset(-21)
        }
        
        let moveX = 150.cgFloat
        
        left_image_view.transform = CGAffineTransform.init(translationX: -moveX, y: 0)
        
        
        view.addSubview(right_image_view)
        right_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(100)
            make.top.equalTo(heart_image_view).offset(-40)
            make.left.equalTo(heart_image_view.snp.right).offset(21)
        }
        
        right_image_view.transform = CGAffineTransform.init(translationX: moveX, y: 0)
        
//        main_thread_execute(afterDelay: 0.3) {
            
            UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 7, options:  UIView.AnimationOptions.allowUserInteraction, animations: {
                    
                self.left_image_view.transform = CGAffineTransform.identity
                self.right_image_view.transform = CGAffineTransform.identity

            }) { (f) in
                
            }
//        }
        
        UIView.animate(withDuration: 0.5, delay: 0.5, options: UIView.AnimationOptions.autoreverse, animations: {
            
            self.heart_image_view.alpha = 1
            
        }) { (f) in
            
        }
        
        /*
        main_thread_execute(afterDelay: 0.5) {
            
            UIView.animate(withDuration: 0.5) {
                
                self.heart_image_view.alpha = 1
            }
        }*/
        
        
    
        guard let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo else { return
        }
        
        
        let nameStr = json["user"]["nickname"].stringValue
        
        
        
        let str = String.init(format: "你和%@一定很多想聊的呢！", nameStr)
        
        pairContentLabel.text = str
        
        
        left_image_view.pin_setImage(from: user.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        print("000000----11---",json["user"]["avatar"].stringValue)
        
        right_image_view.pin_setImage(from: json["user"]["avatar"].stringValue.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        
        main_thread_execute(afterDelay: 1.6) {
            
            BINP.update_base_info(remoteInfo: ST_BTUserInfoFromRemote.init(accid: self.json["user"]["accid"].stringValue, avatar: self.json["user"]["avatar"].stringValue, userName: self.json["user"]["nickname"].stringValue))
                
                let vc = ST_PrivateChatViewController()
                
                vc.sessionID = self.json["user"]["accid"].stringValue
              
                vc.other_chat_source = 1
                
                vc.source = "特质匹配"
            
                var muJson = self.json
                
                muJson["match_message"] = self.json["message"]
                
                vc.matched_header_model = ST_ChatMatchedViewModel.init(json: muJson)
                
                vc.dismiss_oper = true
                
                self.navigationController?.pushViewController(vc)
                
        }
        
    }
    
    
    
    

//    MARK: - Properties
    
    fileprivate lazy var pair_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "恭喜配对成功！"
        control.textColor = Color.white
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 24, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var pairContentLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.white
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()

    fileprivate lazy var heart_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("首页_icon_配对成功")
        control.alpha = 0
        return control
    }()
   
    fileprivate lazy var left_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 100/2.0
        control.borderWidth = 2
        control.borderColor = Color.white
        return control
    }()

    
    fileprivate lazy var right_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 100/2.0
        control.borderWidth = 2
        control.borderColor = Color.white
        return control
    }()
    
    
}
